package com.mapper;

import com.entity.Department;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 部门表数据层
 *
 * @author z 'y
 */
@Mapper
public interface DepartmentMapper {
    /**
     * 返回所有部门的数量
     *
     * @return the int
     */
    @Select("select count(departmentId) from department")
    int countOfDepart();

    /**
     * 查找所有部门表，并实现分页
     *
     * @param startIndex the start index
     * @param pageSize   the page size
     * @return the list
     */
    @Select("select * from department limit #{param2} offset #{param1}")
    List<Department> findPage(int startIndex, int pageSize);

    /**
     * 更新部门信息
     *
     * @param department the department
     * @return the int
     */
    @Update("update department set departmentName = #{departmentName},fatherDepartmentId = #{fatherDepartmentId}," +
            "ranking=#{ranking},basicSalary=#{basicSalary} where departmentId=#{departmentId}")
    int updateDepart(Department department);

    /**
     * 通过部门id找到一个部门信息
     *
     * @param departmentId the department id
     * @return the department
     */
    @Select("select * from department where departmentId=#{departmentId}")
    Department findDepartmentById(long departmentId);

    /**
     * 添加一个部门信息
     *
     * @param department the department
     * @return the boolean
     */
    @Insert("INSERT INTO Department (departmentName, fatherDepartmentId, ranking, basicSalary) " +
            "VALUES (#{departmentName}, #{fatherDepartmentId}, #{ranking}, #{basicSalary})")
    boolean addDepart(Department department);

    /**
     * 获取当前部门id对应的部门的子部门数量
     *
     * @param departmentId the department id
     * @return the depart num by father depart id
     */
    @Select("select count(departmentId) from department where fatherDepartmentId=#{departmentId}")
    int getDepartNumByFatherDepartId(int departmentId);

    /**
     * 删除一个部门通过部门id
     *
     * @param departmentId the department id
     * @return the int
     */
    @Delete("delete from department where departmentId=#{departmentId}")
    int deleteDepartByDepartId(int departmentId);

    /**
     * 获取关键字下的部门条数，关键字可以是部门id或者部门名字
     *
     * @param keys the keys
     * @return the count of depart by keys
     */
    @Select("select count(*) from department where departmentName like concat('%',#{keys},'%') or departmentId like concat('%',#{keys},'%')")
    int getCountOfDepartByKeys(String keys);

    /**
     * 通过关键字查找部门表，并实现分页
     *
     * @param keys       the keys
     * @param startIndex the start index
     * @param pageSize   the page size
     * @return the list
     */
    @Select("select * from department where departmentName like concat('%',#{param1},'%') or departmentId " +
            "like concat('%',#{param1},'%') limit #{param3} offset #{param2}")
    List<Department> findPageByKeys(String keys, int startIndex, int pageSize);

    /**
     * 通过部门ranking返回数量
     *
     * @param ranking the department ranking
     * @return the department
     */
    @Select("select count(*) from department where ranking=#{ranking}")
    int countOfDepartmentByRanking(String ranking);
}
